package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/*
请求处理类
 */
@Slf4j//生成日志对象
@RequestMapping("/depts")
@RestController

public class DeptController {
    @Autowired
    @Qualifier("deptServiceImpl")
    private DeptService deptService;
    @GetMapping
    public Result list() {
        //3.响应数据(json格式)
        return Result.success(deptService.list());
    }

    //删除部门响应处理方法
    //RequestParam加上之后前端必须传递该参数，否则报错400，Bad Request,required属性默认值为true代表前端必须传入参数否则报错
    @DeleteMapping
//    public Result delete(@RequestParam("id") Integer deptId){
   /*
   删除响应处理方法
    */
    @Log
    public Result delete(Integer id){
        log.info("id = {}",id);

        deptService.delete(id);
        return Result.success();
    }

    /*
    插入数据响应处理方法
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Dept dept){
//        System.out.println(dept);
        log.info("dept = {}",dept);
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptService.insert(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("id = "+id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }
/*
修改部门
 */
@Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("dept = {}",dept);
        dept.setUpdateTime(LocalDateTime.now());
        deptService.update(dept);
        return Result.success();
    }
}
